package org.wicketTutorial.CustomResourceMounting;

import org.apache.wicket.DefaultPageManagerProvider;
import org.apache.wicket.Page;
import org.apache.wicket.pageStore.IDataStore;
import org.apache.wicket.pageStore.memory.HttpSessionDataStore;
import org.apache.wicket.pageStore.memory.IDataStoreEvictionStrategy;
import org.apache.wicket.pageStore.memory.MemorySizeEvictionStrategy;
import org.apache.wicket.pageStore.memory.PageNumberEvictionStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.lang.Bytes;
import org.gou.wicketstudy.gae.GaeApplication;
import org.gou.wicketstudy.gae.GaeInitializer;

public class WicketApplication extends WebApplication implements GaeApplication {
	public Class<? extends Page> getHomePage() {
		return HomePage.class;
	}

	private void mountPages() {
		// mount(new MountedMapperWithoutPageComponentInfo("/",
		// ConfirmDeletePage.class));
	}

	@Override
	public void init() {
		mountPages();

		new GaeInitializer().init(this);

		/*
		 * http://apache-wicket.1842946.n4.nabble.com/Wicket-GAE-td3039886.html
		 */
		getResourceSettings().setResourcePollFrequency(null);

		getPageSettings().setVersionPagesByDefault(false);

		/* Store data in the HTTP session. */
		this.setPageManagerProvider(new DefaultPageManagerProvider(this) {
			protected IDataStore newDataStore() {
				IDataStoreEvictionStrategy evictionStrategy = new PageNumberEvictionStrategy(3);
				return new HttpSessionDataStore(getPageManagerContext(),
						evictionStrategy);
			}
		});

		ResourceReference resourceReference = new ResourceReference("rssProducer") {
			private static final long serialVersionUID = 1L;
			RSSProducerResource deviceMetaResource = new RSSProducerResource();

			@Override
			public IResource getResource() {
				return deviceMetaResource;
			}
		};

		mountResource("/foo/bar", resourceReference);
		mountResource("/bar/${baz}", resourceReference);
		getSharedResources().add("globalRSSProducer", resourceReference.getResource());
	}

	@Override
	public IDataStoreEvictionStrategy getEvictionStrategy() {
		return new MemorySizeEvictionStrategy(Bytes.megabytes(2));
	}
}